import { createRouter, createWebHashHistory } from "vue-router";
//import config from "@/utils/config";
export const Layout = () => import("@/layout/index.vue");

import PermissionType from "@/utils/PermissionType";

// 静态路由
export const constantRoutes = [
  {
    path: "/redirect",
    component: Layout,
    meta: { hidden: true },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue"),
        meta:{}
      },
    ],
  },

  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: { hidden: true },
  },

  {
    path: "/",
    name: "/",
    component: Layout,
    redirect: "/dashboard",
    meta:{},
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        // 用于 keep-alive 功能，需要与 SFC 中自动推导或显式声明的组件名称一致
        // 参考文档: https://cn.vuejs.org/guide/built-ins/keep-alive.html#include-exclude
        name: "Dashboard",
        meta: {
          title: "dashboard",
          icon: "homepage",
          affix: true,
          keepAlive: true,
        },
      },
      {
        path: "401",
        component: () => import("@/views/error/401.vue"),
        meta: { hidden: true },
      },
      {
        path: "404",
        component: () => import("@/views/error/404.vue"),
        meta: { hidden: true },
      },
    ],
  },

  // 移除动态路由，添加对应的静态路由用于测试
  {
    path: "/test",
    component: Layout,
    redirect: "/test/test1",
    meta: { title: "测试管理", icon: "system" },
    children: [
      {
        path: "test1",
        name: "Test1",
        component: () => import("@/views/test/test1.vue"),
        meta: { title: "测试1", icon: "el-icon-User", hidden: false, },
      },
      {
        path: "test2",
        name: "Test2",
        component: () => import("@/views/test/test2.vue"),
        meta: { title: "测试2", icon: "role" , hidden: false, permission:['111']},
      },
      {
        path: "test3",
        name: "Test3",
        component: () => import("@/views/test/test3.vue"),
        meta: { title: "测试3", icon: "role", hidden: true},
      },
    ],
  },

  {
    path: "/sys",
    name: 'SysManagement',
    component: Layout,
    redirect: "/sys/org",
    meta: { title: "系统管理", icon: "system" },
    children: [
      {
        path: "org",
        name: "systemOrg",
        component: () => import("@/views/sys/org/org.vue"),
        meta: { title: "组织架构", icon: "el-icon-User", hidden: false, permission:[PermissionType.sysAdmin]},
      },
      {
        path: "orguser",
        name: "systemOrgUser",
        component: () => import("@/views/sys/org/orguser.vue"),
        meta: { title: "组织用户", icon: "el-icon-User", hidden: false, permission:[PermissionType.sysAdmin]},
      },
      {
        path: "role",
        name: "systemRole",
        component: () => import("@/views/sys/role/role.vue"),
        meta: { title: "角色管理", icon: "el-icon-User", hidden: false, permission:[PermissionType.sysAdmin]},
      },
    ],
  },
];

/**
 * 创建路由
 */
const router = createRouter({
  history: createWebHashHistory(),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  scrollBehavior: () => ({ left: 0, top: 0 }),
});

// 全局注册 router
export function setupRouter(app) {
  app.use(router);
}

export default router;
